﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.BaseUI;
using CodeGenerator.Common;
using System.IO;
using ICSharpCode.TextEditor.Document;
using CodeGenerator.Core;
using System.Collections;

namespace CodeGenerator.UI
{
    public partial class FrmTempleteCode : BaseFormEx
    {
        private TemplateFiles entity;
        private string outputDir = string.Empty;
        public FrmTempleteCode(TemplateFiles entity)
        {
            InitializeComponent();

            this.entity = entity;
            

            this.cmb_way.SelectedIndex = 0;
        }

        private void FrmTempleteCode_Load(object sender, EventArgs e)
        {
            StreamReader reader = new StreamReader(DirectoryUtil.FileToStream(entity.FullName));
            
            FormatCodeHelper.FormatText(txt_TempleteFileInfo, "C#", reader.ReadToEnd());

            MainSetting info = Config.GetSetupInfo();
            outputDir = info.OutputDir;
        }

        private void btn_SelectTable_Click(object sender, EventArgs e)
        {
            FrmSelectTable form = new FrmSelectTable();
            form.ShowDialog();
            if (form.selectTable != null)
            {
                List<string> selectTable = form.selectTable;
                ServerInfo serverInfo = Constant.CurrentServerInfo;
                string dbName = Constant.DatabaseName;
                IDatabase dal = DBFactory.Database(serverInfo.DBType);


                string dirPath = outputDir + "\\CustomedTemplate\\";

                foreach (string tableName in selectTable)
                {
                    List<ColumnInfo> list = dal.GetColumnInfoList(serverInfo, dbName, tableName);

                    TableInfo table = new TableInfo();
                    table.Name = tableName;
                    table.Comments = dal.GetTableComments(serverInfo, Constant.DatabaseName, tableName);
                    table.ColumnList = list;

                    string content = RazorHelper.RunCompile("\\CustomedTemplate\\" + entity.FileName, table);
                    if (cmb_way.SelectedIndex == 0)
                    {
                        FrmShowCode form1 = new FrmShowCode(entity.FileType, content);
                        form1.Text = entity.FileName.Split('.')[0] + "-" + tableName;
                        ((FrmMain)(this.ParentForm)).ShowContent(form1.Text, form1);
                    }
                    else
                    {
                        DirectoryUtil.Write(dirPath + "\\" + tableName + entity.Extension, content);
                    }
                    
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                DirectoryUtil.Write(entity.FullName, txt_TempleteFileInfo.Text);
              //  lbl_OperateResult.Text = "文件保存成功";
                MessageUtil.ShowTips("文件保存成功！");
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("保存模板文件信息：", ex);
            }
        }
    }
}
